﻿namespace MDXStudio
{
    using AdvancedDataGridView;
    using Microsoft.AnalysisServices;
    using System;
    using System.Collections.Generic;

    internal abstract class TWEvent
    {
        private long m_Duration;
        private int m_GroupCount = 1;
        internal TWEvent m_Parent = null;
        private float m_RatioToParent;
        private float m_RatioToTotal;
        internal List<TWEvent> m_SubEvents = new List<TWEvent>();
        internal TraceEventArgs m_TraceEventArgs;

        internal TWEvent(TraceEventArgs e)
        {
            this.m_TraceEventArgs = e;
        }

        internal void Add(TWEvent e)
        {
            e.m_Parent = this;
            this.m_SubEvents.Add(e);
        }

        internal virtual int GetImageIndex()
        {
            return 0;
        }

        internal abstract string GetLabel();
        private void Merge(TWEvent e)
        {
            this.Duration += e.Duration;
            this.m_GroupCount += e.m_GroupCount;
        }

        internal void Regroup()
        {
            TWEvent event2 = null;
            List<TWEvent> list = new List<TWEvent>();
            foreach (TWEvent event3 in this.m_SubEvents)
            {
                if (event3.m_SubEvents.Count == 0)
                {
                    if ((event2 != null) && (event2.GetLabel() == event3.GetLabel()))
                    {
                        event2.Merge(event3);
                    }
                    else
                    {
                        list.Add(event3);
                        event2 = event3;
                    }
                }
                else
                {
                    event3.Regroup();
                    list.Add(event3);
                    event2 = null;
                }
            }
            this.m_SubEvents = list;
        }

        internal void UpdateRatios(long TotalDuration, long ParentDuration)
        {
            if (0L == TotalDuration)
            {
                this.m_RatioToTotal = 0f;
            }
            else
            {
                this.m_RatioToTotal = ((float) this.m_Duration) / ((float) TotalDuration);
            }
            if (0L == ParentDuration)
            {
                this.m_RatioToParent = 0f;
            }
            else
            {
                this.m_RatioToParent = ((float) this.m_Duration) / ((float) ParentDuration);
            }
            foreach (TWEvent event2 in this.m_SubEvents)
            {
                event2.UpdateRatios(TotalDuration, this.m_Duration);
            }
        }

        internal void Visualize(TreeGridNode root)
        {
            if (((this.m_SubEvents.Count != 0) || (((base.GetType() != typeof(TWCalculateAxes)) && (base.GetType() != typeof(TWExecuteMdxScript))) && (base.GetType() != typeof(TWSerialize)))) || (this.Duration > 0x10L))
            {
                string str = string.Format("{0:p}", this.RatioToTotal);
                string str2 = string.Format("{0:p}", this.RatioToParent);
                string label = null;
                if (1 == this.m_GroupCount)
                {
                    label = this.GetLabel();
                }
                else
                {
                    label = string.Format("{0} ({1} times)", this.GetLabel(), this.m_GroupCount);
                }
                TreeGridNode node = root.Nodes.Add(new object[] { label, this.Duration.ToString(), str, str2 });
                node.Tag = this.m_TraceEventArgs;
                node.ImageIndex = this.GetImageIndex();
                int num = 0;
                long num2 = 0L;
                foreach (TWEvent event2 in this.m_SubEvents)
                {
                    if (num > 100)
                    {
                        num2 += event2.Duration;
                    }
                    else
                    {
                        event2.Visualize(node);
                    }
                    num++;
                }
                if (num > 100)
                {
                    node.Nodes.Add(new object[] { "More nodes suppressed", num2.ToString(), "", "" });
                }
            }
        }

        internal long Duration
        {
            get
            {
                return this.m_Duration;
            }
            set
            {
                this.m_Duration = value;
            }
        }

        internal int GroupCount
        {
            get
            {
                return this.m_GroupCount;
            }
        }

        internal float RatioToParent
        {
            get
            {
                return this.m_RatioToParent;
            }
        }

        internal float RatioToTotal
        {
            get
            {
                return this.m_RatioToTotal;
            }
        }
    }
}

